package com.wolfsea.windowmanagerdemo;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author liuliheng
 * @desc WindowView2
 * @time 2022/4/17  10:50
 **/
public class WindowView2 extends FrameLayout {

    private static final int ANIMATION_DURATION = 500;

    private boolean onlyOnce = true;
    private final LinearInterpolator mEnterInterpolator = new LinearInterpolator();

    private ObjectAnimator mEnterAnimator;
    private OnShowListener mOnShowListener;
    private OnDismissListener mOnDismissListener;

    public WindowView2(@NonNull Context context) {
        this(context, null);
    }

    public WindowView2(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindowView2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.window_view_layout, this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mOnShowListener != null) {
            mOnShowListener.onShow();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (onlyOnce) {
            mEnterAnimator
                    = ObjectAnimator.ofFloat(this, "translationY", -this.getMeasuredHeight(), 0);
            mEnterAnimator.setInterpolator(mEnterInterpolator);
            mEnterAnimator.setDuration(ANIMATION_DURATION);
            mEnterAnimator.start();

            onlyOnce = false;
        }
    }

    public void hide(boolean removeNow) {
        if (!this.isAttachedToWindow()) {
            return;
        }

        WindowManager windowManager = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null) {
            return;
        }

        if (removeNow) {
            if (this.isAttachedToWindow()) {
                windowManager.removeViewImmediate(this);
                if (mOnDismissListener != null) {
                    mOnDismissListener.onDismiss();
                }
            }
            return;
        }

        findViewById(R.id.body).setClickable(false);

        mEnterAnimator = ObjectAnimator.ofFloat(this, "translationY", 0, -this.getMeasuredHeight());
        mEnterAnimator.setInterpolator(mEnterInterpolator);
        mEnterAnimator.setDuration(ANIMATION_DURATION);
        mEnterAnimator.start();

        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            if (this.isAttachedToWindow()) {
                windowManager.removeViewImmediate(this);
                if (mOnDismissListener != null) {
                    mOnDismissListener.onDismiss();
                }
            }
        }, ANIMATION_DURATION);
    }


    public void setOnShowListener(OnShowListener mOnShowListener) {
        this.mOnShowListener = mOnShowListener;
    }

    public void setOnDismissListener(OnDismissListener mOnDismissListener) {
        this.mOnDismissListener = mOnDismissListener;
    }

    public interface OnShowListener {
        void onShow();
    }

    public interface OnDismissListener {
        void onDismiss();
    }

}
